﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace sistemaAcademicoFinanceiroEscolas
{
    public partial class efetivarPagamentoAluno : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Inserindo a data atual do computador
                txtData.Text = DateTime.Now.ToString("dd/MM/yyyy");

            }
        }

        protected void btnListar_Click(object sender, EventArgs e)
        {
            listar();
        }

        void listar()
        {
            string conteudo = "";

            conteudo = "<table width=\"100%\" border=\"1\" cellpadding=\"5\" cellspacing=\"0\" bordercolor=\"#99CCFF\">";
            conteudo += "<tr>";
            conteudo += "<th>Aluno</th>";
            conteudo += "<th>Data da aula</th>";
            conteudo += "<th>Horário</th>";
            conteudo += "<th>Valor a pagar</th>";
            conteudo += "<th>Pago?</th>";
            //conteudo += "<th>Valor pago</th>";
            conteudo += "</tr>";


            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            string sql = "SELECT codigoAula, codigoProfessor, (select nomeProfessor from professor where aula.codigoProfessor = professor.codigoProfessor) as nomeProfessor, \n";
            sql += "codigoAluno, (select nomeAluno from aluno where aula.codigoAluno = aluno.codigoAluno) as nomeAluno, codigoGrupo, dataAula, horaInicialAula, \n";
            sql += "horaFinalAula, codigoDisciplina, descricaoAssuntoAula, aulaRealizada, observacoesAula,  \n";
            sql += "aulaRealizada, valorHora, pago, aulaCancelada, valorPago, valorTotalSemDesconto, valorTotalComDesconto, codigoAnoLetivo, falta, meiaHora \n";
            sql += "FROM aula WHERE (dataAula = '" + txtData.Text + "') order by CONVERT(INT, SUBSTRING(horaInicialAula,1 , 2))";

            SqlCommand cmdPrincipal = new SqlCommand(sql, sqlc);
            SqlDataReader drPrincipal = cmdPrincipal.ExecuteReader();

            if (drPrincipal.HasRows)
            {
                while (drPrincipal.Read())
                {
                    conteudo += "<tr>";
                    conteudo += "<td align='center'>" + drPrincipal["nomeAluno"].ToString() + "</td>";
                    conteudo += "<td align='center'>" + drPrincipal["dataAula"].ToString() + "</td>";
                    conteudo += "<td align='center'>" + drPrincipal["horaInicialAula"].ToString() + "" + " às " + "" + drPrincipal["horaFinalAula"].ToString() + "</td>";
                    conteudo += "<td align='center'>" + drPrincipal["valorHora"].ToString() + "</td>";


                    if (drPrincipal["pago"].ToString() == "True")
                    {
                        conteudo += "<td align='center'><input id=\"Radio1\" type=\"radio\" CHECKED NAME=\"rbt" + drPrincipal["codigoAula"].ToString() + "\" value=\"rbt1" + drPrincipal["codigoAula"].ToString() + "\" />Pago";
                        conteudo += "<input id=\"Radio2\" type=\"radio\" name=\"rbt" + drPrincipal["codigoAula"].ToString() + "\" value=\"rbt2" + drPrincipal["codigoAula"].ToString() + "\" />Cancelar</td>";


                        //conteudo += "<td align='center'><input type=\"checkbox\" name=\"cbx" + drPrincipal["codigoAula"].ToString() + "\" value=\"cbx" + drPrincipal["codigoAula"].ToString() + "\" checked=checked></td>";
                    }
                    else if (drPrincipal["aulaCancelada"].ToString() == "True")
                    {
                        conteudo += "<td align='center'><input id=\"Radio1\" type=\"radio\" NAME=\"rbt" + drPrincipal["codigoAula"].ToString() + "\" value=\"rbt1" + drPrincipal["codigoAula"].ToString() + "\" />Pago";
                        conteudo += "<input id=\"Radio2\" type=\"radio\" CHECKED name=\"rbt" + drPrincipal["codigoAula"].ToString() + "\" value=\"rbt2" + drPrincipal["codigoAula"].ToString() + "\" />Cancelar</td>";
                    }
                    else
                    {
                        conteudo += "<td align='center'><input id=\"Radio1\" type=\"radio\" NAME=\"rbt" + drPrincipal["codigoAula"].ToString() + "\" value=\"rbt1" + drPrincipal["codigoAula"].ToString() + "\" />Pago";
                        conteudo += "<input id=\"Radio2\" type=\"radio\" name=\"rbt" + drPrincipal["codigoAula"].ToString() + "\" value=\"rbt2" + drPrincipal["codigoAula"].ToString() + "\" />Cancelar</td>";

                        //conteudo += "<td align='center'><input type=\"checkbox\" name=\"cbx" + drPrincipal["codigoAula"].ToString() + "\" value=\"cbx" + drPrincipal["codigoAula"].ToString() + "\"></td>";
                    }

                    //if (drPrincipal["valorPago"].ToString() != "")
                    //{
                    //    conteudo += "<td align='center'><input type=\"text\" name=\"txt" + drPrincipal["codigoAula"].ToString() + "\" value=\"" + drPrincipal["valorPago"].ToString() + "\"></td>";
                    //}
                    //else
                    //{
                    //    conteudo += "<td align='center'><input type=\"text\" name=\"txt" + drPrincipal["codigoAula"].ToString() + "\"></td>";
                    //}


                    conteudo += "</tr>";
                }
            }

            drPrincipal.Close();

            sqlc.Close();
            Panel1.Controls.Add(new LiteralControl(conteudo));
            conteudo += "</table>";

        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            //código de salvar
            //inicio
            //---
            //mensagem
            //fim

            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            string sql = "SELECT codigoAula, codigoProfessor, (select nomeProfessor from professor where aula.codigoProfessor = professor.codigoProfessor) as nomeProfessor, \n";
            sql += "codigoAluno, (select nomeAluno from aluno where aula.codigoAluno = aluno.codigoAluno) as nomeAluno, codigoGrupo, dataAula, horaInicialAula, \n";
            sql += "horaFinalAula, codigoDisciplina, descricaoAssuntoAula, aulaRealizada, observacoesAula,  \n";
            sql += "aulaRealizada, valorHora, pago, aulaCancelada, valorPago, valorTotalSemDesconto, valorTotalComDesconto, codigoAnoLetivo, falta, meiaHora \n";
            sql += "FROM aula WHERE (dataAula = '" + txtData.Text + "') order by CONVERT(INT, SUBSTRING(horaInicialAula,1 , 2))";

            SqlCommand cmdPrincipal = new SqlCommand(sql, sqlc);
            SqlDataReader drPrincipal = cmdPrincipal.ExecuteReader();

            if (drPrincipal.HasRows)
            {
                while (drPrincipal.Read())
                {
                    string cbx = "cbx" + drPrincipal["codigoAula"].ToString();
                    string txt = "txt" + drPrincipal["codigoAula"].ToString();
                    string valorTXT = Request["rbt" + drPrincipal["codigoAula"].ToString()];
                    string valorCBX = Request["rbt" + drPrincipal["codigoAula"].ToString()];

                    string update = "update aula set aulaCancelada=0, pago=0, valorPago='' where codigoAula = " + drPrincipal["codigoAula"].ToString();

                    SqlCommand cmd = new SqlCommand(update, sqld);
                    cmd.ExecuteNonQuery();

                    if (Request["rbt" + drPrincipal["codigoAula"].ToString()] == "rbt1" + drPrincipal["codigoAula"].ToString())
                    {
                        //if (Request["txt" + drPrincipal["codigoAula"].ToString()] != "")
                        //{
                        update = "update aula set pago=1, valorPago='" + drPrincipal["valorHora"].ToString() + "' where codigoAula = " + drPrincipal["codigoAula"].ToString();

                        cmd = new SqlCommand(update, sqld);
                        cmd.ExecuteNonQuery();
                        //}
                    }
                    else if (Request["rbt" + drPrincipal["codigoAula"].ToString()] == "rbt2" + drPrincipal["codigoAula"].ToString())
                    {
                        update = "update aula set aulaCancelada=1, valorPago='" + drPrincipal["valorHora"].ToString() + "' where codigoAula = " + drPrincipal["codigoAula"].ToString();

                        cmd = new SqlCommand(update, sqld);
                        cmd.ExecuteNonQuery();
                    }
                }
            }

            drPrincipal.Close();

            sqlc.Close();
            sqld.Close();

            string literalcc = "<script language=JavaScript>finalizar();</script>";
            Panel1.Controls.Add(new LiteralControl(literalcc));

            listar();
        }
    }
}
